package yunwo.cn.yzstation.view.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.adapter.InventoryAdapter;

import yunwo.cn.yzstation.api.Iswitch;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.bean.InventoryBean;
import yunwo.cn.yzstation.other.PickDialog;
import yunwo.cn.yzstation.other.ShowAnimationDialogUtil;
import yunwo.cn.yzstation.api.IinventoryActivityView;
import yunwo.cn.yzstation.presenter.InventoryActivityPersenter;
import yunwo.cn.yzstation.utils.sharePUtil;
import yunwo.cn.yzstation.view.fregment.inventory.AllFragment;

import yunwo.cn.yzstation.view.fregment.inventory.CompleteddFragment;
import yunwo.cn.yzstation.view.fregment.inventory.InventoryFragment;
import yunwo.cn.yzstation.api.Iswitch2;

import yunwo.cn.yzstation.api.Iswitch3;
import yunwo.cn.yzstation.view.fregment.inventory.OngoinggFragment;

/**
 * Created by lao on 2017/8/23.
 */

public class InventoryActivity extends MyBaseActivity implements View.OnClickListener, IinventoryActivityView {
    private ImageView mBack;
    private TextView mTitlename;
    private TextView mScan;
    private TabLayout mInventoryTab;
    private ViewPager mInventoryVp;
    private ImageView mSwitch1;
    Iswitch mIswitch;
    Iswitch2 mIswitch2;
    Iswitch3 mIswitch3;
    boolean state = true;
    private TextView mStateTV;
    private GridView mGridview;
    private int isF = 0;
    String options = "库存";
    String options1 = "收货人";
    private GridView mGridview2;
    private TextView mConfirm;
    private InventoryActivityPersenter mInventoryActivityPersenter;
    private EditText mConditions;

    private InventoryFragment mInventoryFragment;
    private AllFragment mAllFragment;
    private PickDialog mPickDialog;
    private CompleteddFragment mCompleteddFragment;
    private OngoinggFragment mOngoinggFragment;
    public RelativeLayout mInventory_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initEvent();
    }

    @Override
    protected String settiltile() {
        return "库存管理";
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_inventory;
    }

    public void getIswitch(Iswitch iswitch) {
        this.mIswitch = iswitch;
    }

    public void getIswitch2(Iswitch2 iswitch) {
        this.mIswitch2 = iswitch;
    }

    public void getIswitch3(Iswitch3 iswitch) {
        this.mIswitch3 = iswitch;
    }

    private void initView() {
        mInventoryActivityPersenter = new InventoryActivityPersenter(this, this);
        mBack = (ImageView) findViewById(R.id.back);
        mScan = (TextView) findViewById(R.id.scan);
        mInventoryTab = (TabLayout) findViewById(R.id.inventory_tab);
        mInventoryVp = (ViewPager) findViewById(R.id.inventory_vp);
        mInventory_title = (RelativeLayout) findViewById(R.id.inventory_title);
        mSwitch1 = (ImageView) findViewById(R.id.switch1);
//        mStateTV = (TextView) findViewById(R.id.state);
        Drawable nav_up = getResources().getDrawable(R.drawable.nav_btn_screen);
        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
        mScan.setCompoundDrawables(nav_up, null, null, null);


        boolean state = sharePUtil.getInventorySP(InventoryActivity.this).getBoolean("state", true);
        if (!state) {
            //// TODO: 2017/8/31   列表适配器

            mInventory_title.setVisibility(View.VISIBLE);

        }else {

            mInventory_title.setVisibility(View.GONE);

        }

        mScan.setVisibility(View.VISIBLE);
        mScan.setOnClickListener(this);
        mBack.setOnClickListener(this);
        mSwitch1.setOnClickListener(this);
        state = sharePUtil.getInventorySP(InventoryActivity.this).getBoolean("state", true);
        if (state) {
            mSwitch1.setBackgroundResource(R.drawable.btm_biaozhun);
        } else {
            mSwitch1.setBackgroundResource(R.drawable.btn_list);
        }
    }

    @Override
    public void SearchOrderSuccess(InventoryBean.DataBean dataBean, String orderstate) {
        switch (orderstate) {
            case "库存":
                Toast.makeText(InventoryActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                mInventoryFragment.onSearchOrder(true, dataBean);
                mInventoryVp.setCurrentItem(0);
                mPickDialog.dismiss();
                break;
        }
    }

    private void initEvent() {

        List<Fragment> list = new ArrayList<>();
        mInventoryFragment = new InventoryFragment();
        mOngoinggFragment = new OngoinggFragment();
        mCompleteddFragment = new CompleteddFragment();
        mAllFragment = new AllFragment();
        list.add(mInventoryFragment);
        list.add(mOngoinggFragment);
        list.add(mCompleteddFragment);
        list.add(mAllFragment);
        InventoryAdapter inventoryAdapter = new InventoryAdapter(getSupportFragmentManager(), list);
        mInventoryVp.setAdapter(inventoryAdapter);
        mInventoryVp.setOffscreenPageLimit(1);
        mInventoryTab.setupWithViewPager(mInventoryVp);

     /*   mInventoryTab.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mInventoryTab,28,28);
            }
        });*/
    }

    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.scan:
                showdalog();
                break;
            case R.id.back:
                finish();
                break;
            case R.id.switch1:
                if (state) {
                    state = false;
                    mInventory_title.setVisibility(View.VISIBLE);
                    mSwitch1.setBackgroundResource(R.drawable.btn_list);
                    if (mIswitch != null) {
                        mIswitch.switc(state);
                    }
                    if (mIswitch2 != null) {
                        mIswitch2.switc2(state);
                    }
                    if (mIswitch3 != null) {
                        mIswitch3.switc3(state);
                    }

                } else {
                    state = true;
                    mInventory_title.setVisibility(View.GONE);
                    mSwitch1.setBackgroundResource(R.drawable.btm_biaozhun);
                    if (mIswitch != null) {
                        mIswitch.switc(state);
                    }
                    if (mIswitch2 != null) {
                        mIswitch2.switc2(state);
                    }
                    if (mIswitch3 != null) {
                        mIswitch3.switc3(state);
                    }
                }
                SharedPreferences.Editor iventoryEditor = sharePUtil.getIventoryEditor(InventoryActivity.this);
                iventoryEditor.putBoolean("state", state);
                iventoryEditor.commit();
                break;
        }
    }


    private void showdalog() {
        List<String> list = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        list.add("库存");
        list.add("进行中");
        list.add("已完成");
        list2.add("收货人");
        list2.add("运单号");
        list2.add("手机号");
        mPickDialog = ShowAnimationDialogUtil.showDialog(InventoryActivity.this,
                R.layout.dalog_right, Gravity.RIGHT, true, false);
        View view = mPickDialog.getView();
        mGridview = (GridView) view.findViewById(R.id.gridview);
        mGridview2 = (GridView) view.findViewById(R.id.gridview2);
        mConfirm = (TextView) view.findViewById(R.id.confirm);
        mConditions = (EditText) view.findViewById(R.id.conditions);

        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mInventoryActivityPersenter.SearchOrder(options, options1, mConditions.getText().toString().trim());
            }
        });
        final SaiXuanItemAdapter saiXuanItemAdapter = new SaiXuanItemAdapter(InventoryActivity.this, list);
        final SaiXuanItemAdapter saiXuanItemAdapter1 = new SaiXuanItemAdapter(InventoryActivity.this, list2);
        mGridview.setAdapter(saiXuanItemAdapter);
        mGridview2.setAdapter(saiXuanItemAdapter1);
        mGridview2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                isF = i;
                saiXuanItemAdapter1.notifyDataSetChanged();
                options1 = saiXuanItemAdapter1.getItem(i);
                Toast.makeText(InventoryActivity.this, options1, Toast.LENGTH_SHORT).show();
            }
        });
        mGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                isF = i;
                saiXuanItemAdapter.notifyDataSetChanged();
                options = saiXuanItemAdapter.getItem(i);
                Toast.makeText(InventoryActivity.this, options, Toast.LENGTH_SHORT).show();
            }
        });
    }


    public class SaiXuanItemAdapter extends BaseAdapter {
        Context context;
        List<String> list;
        private int selectedPosition = 0;// 选中的位置

        public SaiXuanItemAdapter(Context context, List<String> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public String getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Myhodler mMyhodler;
            if (view == null) {
                mMyhodler = new Myhodler();
                view = LayoutInflater.from(context).inflate(R.layout.item_saixuan, viewGroup, false);
                mMyhodler.mTextView = (TextView) view.findViewById(R.id.itemTV);
                view.setTag(mMyhodler);
            } else {
                mMyhodler = (Myhodler) view.getTag();
            }
            mMyhodler.mTextView.setText(list.get(i));
            if (i == isF) {
                mMyhodler.mTextView.setBackgroundResource(R.drawable.rightbar_btn_end);
            } else {
                mMyhodler.mTextView.setBackgroundResource(R.drawable.rightbar_btn_normal);
            }
            return view;
        }

        class Myhodler {
            TextView mTextView;
        }


    }

}
